' ects_setup_sql.vbs
'
' Sets up SQL for the External Collaboration Toolkit for SharePoint
'
' Return Codes:
'    0 - Success
'    1 - Could not find OSQL utility
'    2 - Could not find the .SQL file
'    3 - SQL commands did not execute successfully
'    4 - Incorrect arguments provided
'

Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colProcessEnvVars = objShell.Environment("Process")

'
' Parse arguments
'

Set args = Wscript.Arguments
If args.Count = 0 Then
    DisplaySyntax
    Wscript.Quit(4)
End If
    
strSqlServer = args.Item(0)

'
' Set up file names
'

strOSQL = objFSO.BuildPath(colProcessEnvVars("ProgramFiles"), "\Microsoft SQL Server\90\Tools\binn\osql.exe")
strSqlCommandFile = "create_ects_database.sql"

'
' Ensure that all files are where they need to be
'

if Not objFSO.FileExists(strOSQL) Then
    Wscript.Echo("ERROR: Can't find " + strOSQL + ". Is SQL Server 2005 installed?")
    Wscript.Quit(1)
End If

if Not objFSO.FileExists(strSqlCommandFile) Then
    Wscript.Echo("ERROR: Can't find " + strSqlCommandFile + ".  Please verify that this")
    Wscript.Echo("file exists in the current directory.")
    Wscript.Quit(2)
End If

'
' Echo the parameters
'

Wscript.Echo("Running: cscript ects_setup_sql.vbs " + strSqlServer)
Wscript.Echo("")

'
' Run the SQL command file
'

Wscript.Echo("Setting up the ECTS database...")
intReturn = objShell.Run("""" + strOSQL + """ -S " + strSqlServer + " -E -b -i " + strSqlCommandFile, 7, True)

If intReturn = 0 Then
    Wscript.Echo("Done!")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Database setup did not complete successfully.  Script aborting...")
    Wscript.Echo("")
    Wscript.Quit(3)
End If

'
' Done
'

Wscript.Echo("SQL Server setup complete.")
Wscript.Echo("")

Wscript.Quit(0)

'
' Display the syntax for the script
'

Sub DisplaySyntax
    Wscript.Echo("")
    Wscript.Echo("ects_setup_sql.vbs - Set up SQL Server for the ECTS")
    Wscript.Echo("")
    Wscript.Echo("Usage: cscript ects_setup_sql.vbs SQL_Server")
    Wscript.Echo("")
    Wscript.Echo("Where: ""SQL_Server""   is the SQL Server that is being set up, such as")
    Wscript.Echo("                      ""WOODGROVEBANK\SQLEXPRESS"".")
    Wscript.Echo("")
    Wscript.Echo("Notes: [1] The script assumes that SQL is installed in")
    Wscript.Echo("           ""%ProgramFiles%\Microsoft SQL Server"".  If it is not,")
    Wscript.Echo("           please update the script before running.")
    Wscript.Echo("")
    Wscript.Echo("       [2] The script assumes you are running from the directory containing")
    Wscript.Echo("           the SQL command file.")
    Wscript.Echo("")
End Sub
